package com.gtp.mq.demo.spring.config;

import java.util.HashMap;
import java.util.Map;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 声明队列和Exchange,以及相关绑定
 *
 * @author gaotingping@janlc.com
 * @date 2019年8月28日
 */
@Configuration
public class RabbitQueueConfig {

	@Bean
	public Queue queue1() {
		return new Queue("test_queue1");
	}
	
	/*
	 * 队列属性:
	 * String name,名称
	 * boolean durable,是否持久(默认 true:队列将在服务器重启后继续存在) 
	 * boolean exclusive,是否独占队列（队列将仅由声明者使用）
	 * boolean autoDelete, 是否不使用(无消费者)时自动删除
	 * Map<String, Object> arguments 参数
	 */
	/* ################默认Direct模式,发和收都指定队列就成################## */
	@Bean // 最近简单的使用
	public Queue queue2() {
		 
		Map<String, Object> args=new HashMap<String, Object>();
		
		/*
		 * 队列master位置(主副本位置),好像可以在配置文件中指定全局默认“queue_master_locator”
		 * 可选值:
		 * 1.min-masters  分布最少节点
		 * 2.client-local client连接node(好像是默认的) 
		 * 3.random       随机
		 */
		args.put("x-queue-master-locator", "min-masters");
		
		return new Queue("test_queue2",true, false, false,args);
	}

	/**************** FanoutExchange-广播模式 ****************/
	@Bean("Fanout_queue1")
	public Queue queueFanout1() {
		return new Queue("Fanout_queue1");
	}

	@Bean("Fanout_queue2")
	public Queue queueFanout2() {
		return new Queue("Fanout_queue2");
	}

	@Bean("FanoutExchange")
	public FanoutExchange exchangeFanout() {
		return new FanoutExchange("FanoutExchange");
	}

	@Bean
	public Binding bindingFanout1(@Qualifier("Fanout_queue1") Queue queue,
			@Qualifier("FanoutExchange") FanoutExchange exchange) {
		return BindingBuilder.bind(queue).to(exchange);
	}

	@Bean
	public Binding bindingFanout2(@Qualifier("Fanout_queue2") Queue queue,
			@Qualifier("FanoutExchange") FanoutExchange exchange) {
		return BindingBuilder.bind(queue).to(exchange);
	}

	/**************** DirectExchange-按routingkey精确匹配 ****************/
	@Bean("Direct_queue1")
	public Queue queueDirect1() {
		return new Queue("Direct_queue1");
	}

	@Bean("Direct_queue2")
	public Queue queueDirect2() {
		return new Queue("Direct_queue2");
	}

	@Bean("DirectExchange")
	public DirectExchange exchangeDirect() {
		return new DirectExchange("DirectExchange");
	}

	@Bean
	public Binding bindingDirect1(@Qualifier("Direct_queue1") Queue queue,
			@Qualifier("DirectExchange") DirectExchange exchange) {
		return BindingBuilder.bind(queue).to(exchange).with("Direct_queue1");
	}

	@Bean
	public Binding bindingDirect2(@Qualifier("Direct_queue2") Queue queue,
			@Qualifier("DirectExchange") DirectExchange exchange) {
		return BindingBuilder.bind(queue).to(exchange).with("Direct_queue2");
	}

	/**************** TopicExchange-通配符匹配 ****************/
	@Bean("Topic_queue1")
	public Queue queueTopic1() {
		return new Queue("Topic_queue1");
	}

	@Bean("Topic_queue2")
	public Queue queueTopic2() {
		return new Queue("Topic_queue2");
	}

	@Bean("TopicExchange")
	public TopicExchange exchangeTopic() {
		return new TopicExchange("TopicExchange");
	}

	@Bean
	public Binding bindingTopic1(@Qualifier("Topic_queue1") Queue queue,
			@Qualifier("TopicExchange") TopicExchange exchange) {
		return BindingBuilder.bind(queue).to(exchange).with("Topic.k1.*");
	}

	@Bean
	public Binding bindingTopic2(@Qualifier("Topic_queue2") Queue queue,
			@Qualifier("TopicExchange") TopicExchange exchange) {
		return BindingBuilder.bind(queue).to(exchange).with("Topic.k2.*");
	}
}
